//
//  cityVC.m
//  lyxluyou
//
//  Created by mmyy on 15/10/15.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "cityVC.h"
#import "cityItems.h"
#import "UIImageView+AFNetworking.h"
#import "StarView.h"
#import "xiangqingVC.h"
@interface cityVC ()<UIScrollViewDelegate>

@end

@implementation cityVC
{
    UITableView *_leftTableView;
    NSMutableArray *_dataSource;
    UIScrollView *_scrollView;
    NSInteger leftPicH;
    NSInteger leftPicW;
    NSInteger rightPicH;
    NSInteger rightPicW;
    UIImageView *_bgImage;
    int count;
    CGPoint contPoint;

}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    count = 20;
    contPoint = CGPointMake(0, 0);
    self.navigationItem.title = [NSString stringWithFormat:@"热门城市(%@)", self.cityTitle];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    //获取数据
    
    self.view.backgroundColor = [UIColor purpleColor];
    [self createloadDataImage];
    [self loadData];
    [self createSorView];
    leftPicH = 200;
    leftPicW = self.view.frame.size.width / 2 - 10;
    rightPicH = 240;
    rightPicW = self.view.frame.size.width / 2 - 10;
    // Do any additional setup after loading the view.
}
-(void)createloadDataImage
{
    _bgImage = [loadImage loadImage];
    _bgImage.frame = CGRectMake((self.view.frame.size.width - 100) / 2.0, (self.view.frame.size.height - 100) / 2, 100, 100);
    [self.view addSubview:_bgImage];
}
-(void)loadData
{
    _bgImage.hidden = NO;
    [self.view bringSubviewToFront:_bgImage];
    _dataSource = [NSMutableArray arrayWithCapacity:0];
    NSString *url = [NSString stringWithFormat:@"%@%@%@",remenUrl, self.cityId, [NSString stringWithFormat:appendRemen, count]];
    [Funcation getWithURL:url success:^(id responseObject) {
        _bgImage.hidden = YES;
        [_dataSource removeAllObjects];
        for (UIView *view in _scrollView.subviews) {
            [view removeFromSuperview];
        }
        NSArray *dataArray = [responseObject objectForKey:@"items"];
        for (NSDictionary *dict in dataArray) {
            cityItems *items = [cityItems cityItemsWithDict:dict];
            if (![items.cover_s isKindOfClass:[NSNull class]] && ![items.name isKindOfClass:[NSNull class]]) {
                [_dataSource addObject:items];
            }
            
        }
        [self createView];
        [_scrollView footerEndRefreshing];
    } andFail:^(id error) {
        NSLog(@"失败");
    }];


}
-(void)createSorView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    
}
-(void)createView
{
    CGFloat leftH = 0;
    CGFloat rightH = 0;
    for (int i = 0; i < _dataSource.count; i++) {
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor orangeColor];
        bgView.tag = i + 10;
        bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChange:)];
        [bgView addGestureRecognizer:tap];
        [_scrollView addSubview:bgView];
        cityItems *items = [_dataSource objectAtIndex:i];
        UIImageView *bgImage = [[UIImageView alloc] init];
        if (![items.cover_s isKindOfClass:[NSNull class]]) {
            [bgImage setImageWithURL:[NSURL URLWithString:items.cover_s] placeholderImage:[UIImage imageNamed:@"define"]];
        }
        [bgView addSubview:bgImage];
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor blackColor];
        view.alpha = 0.6;
        [bgImage addSubview:view];
        UILabel *name = [[UILabel alloc] init];
        name.adjustsFontSizeToFitWidth = YES;
        name.textColor = [UIColor redColor];
        name.font = [UIFont boldSystemFontOfSize:15];
        if (![items.name isKindOfClass:[NSNull class]]) {
            name.text = items.name;
        }
        
        [view addSubview:name];
        
        StarView *starView = [[StarView alloc] init];
        [starView setStarValue:[items.rating floatValue] / 5.0];
        [view addSubview:starView];
        
        UILabel  * recommended_reason = [[UILabel alloc] init];
        recommended_reason.textColor = [UIColor orangeColor];
        recommended_reason.font = [UIFont systemFontOfSize:10];
        recommended_reason.adjustsFontSizeToFitWidth = YES;
        recommended_reason.lineBreakMode = NSLineBreakByWordWrapping;
        recommended_reason.numberOfLines = 3;
        if (![items.recommended_reason isKindOfClass:[NSNull class]]) {
            recommended_reason.text = items.recommended_reason;
        }
        
        [view addSubview:recommended_reason];
        CGFloat height = i % 2 ? 180: 220;
        if (leftH <= rightH) {
            [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(leftPicW);
                make.height.equalTo(height);
                make.top.equalTo(_scrollView.top).offset(leftH + 5);
                make.left.equalTo(_scrollView.left).offset(5);
            }];
            leftH += (height + 5);
            
        }else {
            [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(leftPicW);
                make.height.equalTo(height);
                make.top.equalTo(_scrollView.top).offset(rightH + 5);
                make.left.equalTo(_scrollView.left).offset(5 + leftPicW + 10);
            }];
            rightH += (height + 5);
        }
        if (i == _dataSource.count - 1) {
            [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_scrollView.bottom).offset(-20);
            }];
        }
        [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView.mas_top).offset(0);
            make.left.equalTo(bgView.mas_left).offset(0);
            make.right.equalTo(bgView.mas_right).offset(0);
            make.bottom.equalTo(bgView.mas_bottom).offset(0);
            
        }];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(60);
            make.left.equalTo(bgImage.mas_left).offset(0);
            make.right.equalTo(bgImage.mas_right).offset(0);
            make.bottom.equalTo(bgImage.mas_bottom).offset(0);
        }];
        //
        [name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(5);
            make.left.equalTo(view.mas_left).offset(10);
            make.right.equalTo(view.mas_right).offset(-10);
            make.bottom.equalTo(starView.mas_top).offset(-5);
            make.height.equalTo(@15);
        }];
        
        [starView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(name.mas_bottom).offset(5);
            make.left.equalTo(view.mas_left).offset(10);
            make.right.equalTo(view.mas_right).offset(-10);
            make.bottom.equalTo(recommended_reason.mas_top).offset(-5);
            make.height.equalTo(@10);
        }];
        
        [recommended_reason mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(starView.mas_bottom).offset(5);
            make.left.equalTo(view.mas_left).offset(10);
            make.right.equalTo(view.mas_right).offset(0);
            make.bottom.equalTo(view.mas_bottom).offset(0);
        }];
        //
    }
    __weak typeof(self) isSelf = self;
    [_scrollView addFooterWithCallback:^{
        NSLog(@"aaaa");
        count += 10;
        contPoint = _scrollView.contentOffset;
        [isSelf loadData];
    }];
    [_scrollView setContentOffset:contPoint animated:NO];
    
}
-(void)tapChange:(UIGestureRecognizer *)sender
{
    UIView *view = (UIView *)sender.view;
    cityItems *items = [_dataSource objectAtIndex:view.tag - 10];
    xiangqingVC *vc = [xiangqingVC new];
    vc.vid = items.vid;
    vc.titleName = items.name;
    [self.navigationController pushViewController:vc animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
